import 'dart:convert';

import 'package:convert/convert.dart';
import 'package:crypto/crypto.dart';
import 'package:share_fit/app/helper/ext/tr_ext.dart';
import 'package:share_fit/generated/locales.g.dart';

class HttpConst {

  // static String HTTP_BASE = 'http://58.34.52.50:40111';
  static String HTTP_BASE = 'https://watchapp.wiodo.tech';

  ///个人用户发送验证码接口
  // static String sendVerifyCode ='$HTTP_BASE/api/v3/ucaaa/regist/sendVerifyCode';
  static String sendVerifyCode ='$HTTP_BASE/api/aaa/regist/sendVerifyCode';
  ///个人用户注册接口
  // static String regist ='$HTTP_BASE/api/v3/ucaaa/regist/regist';
  // static String regist ='$HTTP_BASE/api/aaa/regist/regist';
  static String regist ='$HTTP_BASE/api/v3/ucaaa/regist/regist';
  ///用户登录认证
  static String tokens ='$HTTP_BASE/api/v2/tokens';
  ///密码找回
  static String forgetPwd ='$HTTP_BASE/api/aaa/regist/forgetPwd';
  ///账户注销
  static String regist_del ='$HTTP_BASE/api/aaa/regist/regist';
  ///密码校验
  static String check_pwd ='$HTTP_BASE/api/aaa/regist/checkPwd';

  static String generateMD5(String data) {
    var content = const Utf8Encoder().convert(data);
    var digest = md5.convert(content);
    return hex.encode(digest.bytes);
  }

  bool phoneNumber(String input) {
    if (input.isEmpty) return false;
    //手机正则验证
    String regexPhoneNumber = "^((13[0-9])|(15[^4])|(166)|(17[0-8])|(18[0-9])|(19[8-9])|(147,145))\\d{8}\$";
    return RegExp(regexPhoneNumber).hasMatch(input);
  }

  static bool email(String input) {
    if (input.isEmpty) return false;
    // 邮箱正则
    String regexEmail = "^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*\$";
    return RegExp(regexEmail).hasMatch(input);
  }

  static bool verifyPassword(String input) {
    if (input.isEmpty) return false;
    // String regexPassword = "^(?=.*[A-Za-z])(?=.*\\d)[A-Za-z\\d]{8,}\$";
    String regexPassword = "^[a-zA-Z](?=.*\\d)[A-Za-z\\d]{7,}\$";
    print(regexPassword);
    return RegExp(regexPassword).hasMatch(input);
  }

  static String getCodeToString(int code) {
    String result = 'unknown error：$code';
    switch(code) {
      case 400:
        result = LocaleKeys.bad_request.ts;
        break;
      case 401:
        result = LocaleKeys.unauthorized.ts;
        break;
      case 403:
        result = LocaleKeys.login_expired_please_login_again.ts;
        break;
      case 404:
        result = LocaleKeys.not_found.ts;
        break;
      case 500:
        result = LocaleKeys.server_error.ts;
        break;
      case 504:
        result = LocaleKeys.gateway_timeout.ts;
        break;
      case 4003:
        result = LocaleKeys.permission_forbidden.ts;
        break;
      case 4004:
        result = LocaleKeys.please_register_user.ts;
        break;
      case 5019:
        result = LocaleKeys.user_not_exist.ts;
        break;
      case 35003:
        result = LocaleKeys.internal_server_error.ts;
        break;
      case 60101:
        result = LocaleKeys.register_unsourced.ts;
        break;
      case 60102:
        result = LocaleKeys.register_repeat_send_code_error.ts;
        break;
      case 60103:
        result = LocaleKeys.register_username_check_fail.ts;
        break;
      case 60104:
        result = LocaleKeys.register_username_conflict.ts;
        break;
      case 60105:
        result = LocaleKeys.register_username_not_exist.ts;
        break;
      case 60106:
        result = LocaleKeys.register_code_expire_fail.ts;
        break;
      case 60107:
        result = LocaleKeys.register_password_conflict.ts;
        break;
      case 60108:
        result = LocaleKeys.register_password_check_fail.ts;
        break;
      case 60109:
        result = LocaleKeys.register_token_check_fail.ts;
        break;
      case 60110:
        result = LocaleKeys.register_code_check_fail.ts;
        break;
      case 60113:
        result = LocaleKeys.register_origin_password_check_fail.ts;
        break;
      case 60001:
        result = LocaleKeys.timeout_error.ts;
        break;
      case 60002:
        result = LocaleKeys.sign_error.ts;
        break;
      case 60003:
        result = LocaleKeys.miss_column_error.ts;
        break;
      case 60004:
        result = LocaleKeys.invalid_parameter_error.ts;
        break;
      case 60009:
        result = LocaleKeys.user_device_not_exist_error.ts;
        break;
      case 60099:
        result = LocaleKeys.system_error.ts;
        break;
    }

    return result;
  }

}